﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Utils.Common;
using ZK.BLL;
using ZK.BLL.Config;

namespace ZK.Client
{
    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            bool createNew;

            BindExceptionHandler();//绑定程序中的异常处理

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            try
            {
            
                using (System.Threading.Mutex mutex = new System.Threading.Mutex(true, Application.ProductName, out createNew))
                {

                    //if (!SoftAuthorize.Instance.IsRegistered(AppSettingsConfig.RegisterCode))
                    //{
                    //    string mc = SoftAuthorize.Instance.GetMachineCodeString();
                    //    string msg = $"本机机器码:[{mc}],请先完成注册!";

                    //    FrmRegisterInfo frm = new FrmRegisterInfo(msg);
                    //    if (frm.ShowDialog() != DialogResult.OK)
                    //        System.Environment.Exit(1);
                    //}

                    Class_Config.Station = LocalMachineHelper.GetCPUSerialNumber()+"+"+ LocalMachineHelper.GetHardDiskID();
                    Class_Config.PrinetType = System.Configuration.ConfigurationManager.AppSettings["PrinetType"].ToString();
                    if (createNew)
                    {
                        FrmPassword frm = new FrmPassword();
                        if (frm.ShowDialog() == DialogResult.OK)
                        {

                            Application.Run(new FrmDefault());
                        }
                        else {
                            System.Environment.Exit(1);
                        }
                    }
                    else
                    {
                        MessageBox.Show("应用程序已经在运行中");
                        System.Threading.Thread.Sleep(1000);
                        System.Environment.Exit(1);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());

            }

        }


        /// <summary>
        /// 绑定程序中的异常处理
        /// </summary>
        private static void BindExceptionHandler()
        {
            //设置应用程序处理异常方式：ThreadException处理
            Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
            //处理UI线程异常
            Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
            //处理未捕获的异常
            AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
        }
        /// <summary>
        /// 处理UI线程异常
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
        {
            Exception exception = e.Exception as Exception;
            LogHelper.LogError( exception);
            //string str = GetExceptionMsg(exception);
            MessageBox.Show(exception.Message);
        }
        /// <summary>
        /// 处理未捕获的异常
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
 
            Exception exception = e.ExceptionObject as Exception;
            LogHelper.LogError(exception);
            //string str = GetExceptionMsg(exception);
            MessageBox.Show(exception.Message);
        }


        /// <summary>
        /// 生成自定义异常消息
        /// </summary>
        /// <param name="ex">异常对象</param>
        /// <param name="backStr">备用异常消息：当ex为null时有效</param>
        /// <returns>异常字符串文本</returns>
        static string GetExceptionMsg(Exception ex)
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("****************************异常文本****************************");
            if (ex != null)
            {
                sb.AppendLine("【异常信息】：" + ex.Message);
                sb.AppendLine("【堆栈调用】：" + ex.StackTrace);
            }
            sb.AppendLine("*****************************************************************");
            return sb.ToString();
        }

    }
}
